package vsvm.graphics.charts;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Rectangle;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;

import vsvm.data.model.AbstractAttribute;
import vsvm.data.model.AbstractDataModel;
import vsvm.math.statistics.Characteristics;

public class CorrelationChart extends JScrollPane{
	private static final long serialVersionUID = 100L;
	
	double[][] coefs = null;
	JPanel backPanel = new JPanel();
	
	public CorrelationChart(AbstractDataModel dataModel, ArrayList <AbstractAttribute> attributes) {
		
		int n = attributes.size();
		coefs = new double[n][n];
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				coefs[i][j] = coefs[j][i] = Characteristics.getSpearmanCorrelation(
												dataModel.getAttributeData(attributes.get(i)),
												dataModel.getAttributeData(attributes.get(j))
											);
			}
		}
		
		backPanel.setBackground(Color.WHITE);
		backPanel.setLayout(new GridLayout(n + 1, n + 1));
		
		GridLayout layout = new GridLayout();	
		
		JPanel crossPanel = new JPanel();
		crossPanel.setBackground(Color.WHITE);
		crossPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));		
		backPanel.add(crossPanel);
		
		JPanel cellPanel;
		for (int i = 1; i < n + 1; i++){
			cellPanel = new TitleCell(attributes.get(i - 1).getName());
			cellPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));		
			backPanel.add(cellPanel);
		}
		
		for (int i = 1; i < n + 1; i++){		
			cellPanel = new TitleCell(attributes.get(i - 1).getName());
			cellPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));			
			backPanel.add(cellPanel);	
			for (int j = 1; j < n + 1; j++){
				cellPanel = new CorrelationCell(coefs[i - 1][j - 1]);
				cellPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
				cellPanel.setToolTipText("(" + i + ", " + j + ")");
				backPanel.add(cellPanel);
			}
		}
		
		this.getViewport().add(backPanel);
			
		
	}	
}

class TitleCell extends JPanel {
	
	private String title = null;	
	
	TitleCell(String title) {
		this.title = title;		
		
		this.setLayout(new BorderLayout());		
		this.add(new JLabel(title, JLabel.CENTER), BorderLayout.CENTER);	
		
		this.setBackground(Color.WHITE);
	}	
}

class CorrelationCell extends JPanel {
	
	
	public static final Color  STRONG_CORR_COLOR = Color.RED;
	public static final Color  WEEK_CORR_COLOR   = Color.GREEN;
	public static final double STRONG_CORR_LIMIT = 0.6;
	
	private double correlation = 0.0;
	private DecimalFormat df   = null;
	
	CorrelationCell(double correlation) {
		this.correlation = correlation;		
			
		this.setLayout(new BorderLayout());
		this.setBackground(Color.WHITE);
		
		DecimalFormatSymbols dfs = new DecimalFormatSymbols();
		dfs.setDecimalSeparator('.');
		df = new DecimalFormat("0.00", dfs);
		
		this.add(new JLabel(df.format(correlation), JLabel.CENTER), BorderLayout.NORTH);
		
		Color curColor =  this.WEEK_CORR_COLOR;
		if (correlation > this.STRONG_CORR_LIMIT) curColor = this.STRONG_CORR_COLOR;
		
		this.add(new BarPanel(curColor, correlation), BorderLayout.CENTER);
	}
	
	private class BarPanel extends JPanel {
		
		private Color    c = null;
		private double cor = 0;		
		
		BarPanel(Color c, double correlation) {
			this.c = c;
			this.cor = correlation;
			this.setBackground(Color.WHITE);			
		}
		
		public void paint(Graphics g) {				
			
			Rectangle bounds = this.getBounds();
			g.setColor(this.c);
			int lux = (int)(bounds.height*(1 - Math.abs(cor))); 
			g.fillRect(0, lux, bounds.width, bounds.height - lux);
			
		}
		
		public void update(Graphics g){
			repaint();
		}
	}
}
